package com.hk.gulimall.ware.service.impl;

import com.hk.common.utils.R;
import com.hk.gulimall.ware.feign.ProductFeignService;
import com.hk.gulimall.ware.vo.SkuHasStockVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.common.utils.PageUtils;
import com.hk.common.utils.Query;

import com.hk.gulimall.ware.dao.WareSkuDao;
import com.hk.gulimall.ware.entity.WareSkuEntity;
import com.hk.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * skuId: 1
         * wareId: 2
         */
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String)params.get("skuId");
        if(!StringUtils.isNullOrEmpty(skuId)){
            queryWrapper.eq("sku_id",skuId);
        }

        String wareId = (String)params.get("wareId");
        if(!StringUtils.isNullOrEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        WareSkuEntity wareSkuEntity = wareSkuDao.selectOne(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntity != null) {
            Integer stock = wareSkuEntity.getStock();
            wareSkuEntity.setStock(stock + skuNum);
            wareSkuDao.updateById(wareSkuEntity);
        }else{
            WareSkuEntity entity = new WareSkuEntity();
            entity.setSkuId(skuId);
            entity.setWareId(wareId);
            entity.setStock(skuNum);
            //远程查询Sku的名字
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                if(info.getCode() == 0) {
                    entity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){

            }

            wareSkuDao.insert(entity);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> vos = skuIds.stream().map(skuId -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            vo.setSkuId(skuId);
            //查询当前sku的总库存量
            List<WareSkuEntity> wareSkuEntities = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId));
            Long sum = 0L;
            for (WareSkuEntity wareSkuEntity : wareSkuEntities) {
                sum += (wareSkuEntity.getStock() - wareSkuEntity.getStockLocked());
            }
            vo.setHasStock(sum > 0);
            return vo;
        }).collect(Collectors.toList());

        return vos;
    }

}